faqcat = {}

faqcat.list = function(){
    main.init();
    fly.ajax({
        service: '/faqCategory/list',
        success: function(result){
            if(result.status){
                $("#content").html(fly.template('http://naima.vn/faqcat/list.html',{
                    data: result.data
                }));                
            } else {
                popup.msg(result.message);
            }
        }
    });
    
}


faqcat.load = function(){
    fly.ajax({
        service: '/faqCategory/list',
        success: function(result){
            if(result.status){
                //Hiển thị danh sách
                $("#content").html(fly.template('http://naima.vn/faqcat/list.html',{
                    data: result.data
                })); 
            } else {
                
            }
        }
    });
    
}

faqcat.create = function(){
    popup.open('popup-faqcat-form', 'Thêm danh mục mới', fly.template('http://naima.vn/faqcat/form.html',{
        data: null
    }),[
    {
        title: 'Thêm mới',
        fn:function(){
            fly.submit({
                id: 'faqcat-form',
                service: '/faqCategory/create',
                success: function(result){
                    popup.msg(result.message);
                    faqcat.load();
                    popup.close('popup-faqcat-form');
                }
            });
        }
    },
    {
        title: 'Hủy bỏ',
        fn:function(){
            popup.close('popup-faqcat-form');
        }
    },
    ]);
}

faqcat.edit = function(id){
    fly.ajax({
        service: '/faqCategory/get/',
        data: {
            id: id
        },
        success: function(result){
            popup.open('popup-faqcat-form', 'Sửa danh mục', fly.template('http://naima.vn/faqcat/form.html',{
                data:result.data
            }),[
            {
                title: 'Sửa',
                fn:function(){
                    fly.submit({
                        id: 'faqcat-form',
                        service: '/faqCategory/edit',
                        success: function(rs){
                            popup.msg(rs.message);
                            faqcat.load();
                            popup.close('popup-faqcat-form');
                        }
                    });
                }
            },
            {
                title: 'Hủy bỏ',
                fn:function(){
                    popup.close('popup-faqcat-form');
                }
            }
            ]);
            
        }
        
    });
     
}

faqcat.del = function(id){
    popup.confirm("Bạn có chắc muốn xóa danh mục này ?", function(){
        fly.ajax({
            service: '/faqCategory/delete',
            data: {
                id:id
            },
            success: function(result){
                popup.msg(result.message);
                $(".table tr[for="+result.data.id+"]").remove();
            }
        });
    });
    
}

faqcat.changeStatus = function(id){
    fly.ajax({
        service: '/faqCategory/changeStatus/',
        data: {
            id: id
        },
        success: function(data){
            if(data.status){
                if(data.data == 1)
                    $('.published-'+id).attr('src',fly.assetsUrl + '/images/icon-enable.png');
                else
                    $('.published-'+id).attr('src',fly.assetsUrl + '/images/icon-disable.png');
            }
            else
                popup.msg(data.message);
        }
    });
}